package com.zang.blogz.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zang.blogz.annotation.OptLog;
import com.zang.blogz.constant.OptTypeConstant;
import com.zang.blogz.entity.Tag;
import com.zang.blogz.model.output.TagArticleVO;
import com.zang.blogz.model.output.TagViewVO;
import com.zang.blogz.model.output.admin.AdminTagVO;
import com.zang.blogz.result.Result;
import com.zang.blogz.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朵橙i
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/blogz/tag")
@Api(tags = "标签模块")
public class TagController {
    @Autowired
    private TagService tagService;

    @ApiOperation(value = "获取标签页所有信息", notes = "获取标签页所有信息")
    @GetMapping("")
    public Result<TagViewVO> getTagViewInfo() {

        return Result.ok(tagService.getTagViewInfo());
    }

    @ApiOperation(value = "根据id查询标签下所有文章", notes = "根据id查询标签下所有文章")
    @GetMapping("/tagId")
    public Result<List<TagArticleVO>> getCategoryArticleVoById(@RequestParam("tagId") Integer tagId) {

        return Result.ok(tagService.getTagArticleVoById(tagId));
    }

    @ApiOperation(value = "添加或编辑标签", notes = "添加或编辑标签")
    @PostMapping("/admin/saveOrEdit")
    @OptLog(optType = OptTypeConstant.SAVE_OR_UPDATE)
    public Result<Tag> saveOrEdit(@RequestBody Tag tag) {

        return Result.ok(tagService.saveOrEdit(tag));
    }

    @ApiOperation(value = "获取管理员页标签分页列表", notes = "获取管理员页标签分页列表")
    @GetMapping("/admin/page")
    public Result<IPage<AdminTagVO>> getTagPage(@RequestParam(value = "categoryName", required = false) String tagName,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        Page<Tag> page = new Page<>(pageNo, pageSize);

        return Result.ok(tagService.getTagPage(tagName, page));
    }

    @ApiOperation(value = "删除标签", notes = "删除标签")
    @DeleteMapping("/admin/deleteByTagId")
    @OptLog(optType = OptTypeConstant.REMOVE)
    public Result<String> deleteByTagId(@RequestParam(value = "tagId") Integer tagId) {

        return tagService.deleteByTagId(tagId);
    }

}
